package com.main.controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.generico.dao.ctg.CtgCatalogoDao;
import com.generico.dto.ctg.CtgCatalogo;
import com.generico.dto.ctg.CtgProducto;
import com.generico.exception.AsiWebException;
import com.main.cliente.dto.SgiPersona;
import com.main.dao.AsistenciaTecnicaDAO;
import com.main.dao.ClienteDAO;
import com.main.dao.InventarioDao;
import com.main.dao.SgiPersonaDao;
import com.main.java.AsistenciaTecnica;
import com.main.java.Cliente;
import com.main.java.Empleado;
import com.main.java.Inventario;
import com.main.java.Producto;
import com.main.service.AsistenciaTecnicaService;
import com.web.security.CustomUser;
import com.web.util.GenericoUtil;

@Controller
@RequestMapping("/empleado")
public class ControlInventarioController extends BaseController{
	@Autowired
	private InventarioDao inventarioDao;
		
	@Autowired
	private CtgCatalogoDao ctgCatalogoDao;
	
	@RequestMapping(value = "/controlInventario")
	public String controlInventario(HttpServletRequest request, ModelMap model) {
//		List<Object[]> materiaPrima = ctgCatalogoDao.findAllActivesAsArray(GenericoUtil.CTG_MATERIAL_ALFOMBRA);
//		materiaPrima.addAll(ctgCatalogoDao.findAllActivesAsArray(GenericoUtil.CTG_MATERIAL_CORTINA));
		model.put("userName", GenericoUtil.getUserName());
		model.put("usuarioId", GenericoUtil.getSgdUsuarioIdFromAcegi());
		model.put("inventario", JSONArray.fromObject(inventarioDao.obtenerInventarioParaControlInventario()));
		model.put("materiaPrima", JSONArray.fromObject(inventarioDao.obtenerInventario()));
		model.put("rol", GenericoUtil.getRol().toString());
		return "empleado/controlInventario";
		
	}
	
	@RequestMapping(value = "/controlInventario/save", method = {RequestMethod.POST})
	public void save(HttpServletResponse response,
			@RequestParam(value="inventarioId") Long inventarioId,
			@RequestParam(value="cantidad") String cantidad,
			@RequestParam(value="materiaPrimaId") Long materiaPrimaId) {
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			
			
			
			if(materiaPrimaId != null){
				
				
				if (inventarioId == null ) {
					
					 Inventario inventario = new Inventario();
					 inventario.setInventarioCantidad(cantidad);
					 inventario.setCtgMateriaPrimaId(ctgCatalogoDao.findByCtgCatalogoId(materiaPrimaId));
					 inventarioDao.guardarOActualizarInventario(inventario);
						result.put(SUCCESS, true);
						result.put(MESSAGE, "Datos de Inventario Guadados Correctamente");
						List<Object[]> list = inventarioDao.obtenerInventario();
						for (int index = 0; index < list.size(); index++) {
							if(inventario.getInventariId().equals(list.get(index)[0])){
								result.put("controlInventarioIndex", index);
								break;
							}
						}
						result.put("controlInventarios", JSONArray.fromObject(list));
				}
				
				
				
				
				if (inventarioId != null) {
					Inventario inventario = new Inventario();
					inventario = inventarioDao.obtenerInventarioPorId(inventarioId);
					inventario.setInventarioCantidad(cantidad);
					inventarioDao.guardarOActualizarInventario(inventario);
					
					result.put(SUCCESS, true);
					result.put(MESSAGE, "Datos de Inventario Guadados Correctamente");
					List<Object[]> list = inventarioDao.obtenerInventarioParaControlInventario();
					for (int index = 0; index < list.size(); index++) {
						if(inventario.getInventariId().equals(list.get(index)[0])){
							result.put("controlInventarioIndex", index);
							break;
						}
					}
					result.put("controlInventarios", JSONArray.fromObject(list));
				}
				
				
				
				
			}else{
				result.put(FAILURE, true);
				result.put(MESSAGE, GenericoUtil.ERROR_MESSAGE);
			}
		}catch(Exception e){
			result.put(FAILURE, true);
			result.put(MESSAGE, GenericoUtil.getExceptionMessage(e));
		}
		serializeObject(result, response);
	}
}
	
	
	
	
	

